package com.JL.engine.View;

import com.JL.engine.components.PositionComponent;
import com.JL.engine.model.Entity;
import com.JL.engine.model.View;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

public class ImageView implements View {
    private PositionComponent offset =new PositionComponent();
    private int zindex;
    private Image image;
    public ImageView(Image image){this.image=image;}
    @Override
    public Point2D getOffset(){return offset.getPosition();}

    public void setPosition(Point2D position){offset.setPosition(position);}

    @Override
    public int getZIndex() {
        return zindex;
    }

    @Override
    public void draw(Graphics g, Entity entity) {
        g.drawImage(image, (int)(entity.getX()+ offset.getX()),(int)(entity.getY()+offset.getY()),entity.getWidth(),entity.getHeight(),null);
    }
    public ImageView subImageView(int x,int y,int width,int height){
        //创建新的空白图片
        BufferedImage image=new BufferedImage(width,height,((BufferedImage)this.image).getType());
        //得到新图片的Graphics2D
        Graphics g= image.getGraphics();
        g.drawImage(this.image,0,0,width,height,x,y,x+width,y+height,null);
        g.dispose();
        return new ImageView(image);
    }
    public ImageView subImageView(Rectangle rectangle){
        return subImageView(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
    }
}